﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CRMSystemV2.SQLServerDAL
{
    /// <summary>
    /// 统一集中管理查询条件
    /// </summary>
    public abstract class DALBase:Pager
    {
        /// <summary>
        /// 用于拼接条件语句的辅助字符串
        /// </summary>
        private readonly string strPublic = "1=1";
        /// <summary>
        /// 用于返回sql语句时替换掉该辅助字符串
        /// </summary>
        private readonly string strReplace = "1=1 and ";
        /// <summary>
        /// 拼接搜索关键值 字符串
        /// </summary>
        /// <param name="keyWords"></param>
        /// <param name="orderField"></param>
        /// <param name="orderDirection"></param>
        /// <returns></returns>
        protected virtual string GetStringWhere(Dictionary<string, string> keyWords)
        {            
            return string.Empty;
        }
        /// <summary>
        /// 拼接排序 字符串
        /// </summary>
        /// <param name="orderField"></param>
        /// <param name="orderDirection"></param>
        /// <returns></returns>
        protected string GetStringOrderby(string orderField, string orderDirection)
        {
            if (!string.IsNullOrEmpty(orderField) && !string.IsNullOrEmpty(orderDirection))
                return string.Format(" order by [{0}] {1}", orderField, orderDirection);
            return string.Empty;
        }
        /// <summary>
        /// 用于替换掉拼接字符串时的辅助字符串
        /// </summary>
        /// <param name="sb"></param>
        /// <returns></returns>
        protected string GetReplacedWhere(StringBuilder sb)
        {
            return sb.ToString() == strPublic ? string.Empty : sb.Replace(strReplace, "").ToString();
        }
        protected StringBuilder CreateStringBuilder()
        {
            return new StringBuilder(strPublic);
        }
    }    
}
